package com.system.presideover.utils;

import com.alibaba.fastjson.JSONObject;
import com.system.common.result.Result;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进行token拦截");
        System.out.println(request.getRequestURI());
        String token = request.getHeader("token");

        //判断令牌是否存在，如果不存在，返回错误结果（未登录）
        if (!StringUtils.hasLength(token)){
            //         根据请求的URI进行判断
//            String requestURI = request.getRequestURI();
//            if (requestURI.equals("/getMessageBoxByStatus") || requestURI.equals("/getAllUserRoom") || requestURI.equals("/getAllCarousel") || requestURI.equals("/getAllForum") || requestURI.equals("/getFeedInfoBySelect") || requestURI.equals("/files")) {
//                log.info("该接口令牌不存在也能看");
//                return true;
//            }

            String requestURI = request.getRequestURI();
            if (requestURI.equals("/presideover/emcee/emceelsl/**") || requestURI.equals("/presideover/slideshow/listSlideshow") || requestURI.equals("/presideover/auction-calendar/listAuctionCalendar") || requestURI.equals("/presideover/account-number/login") || requestURI.equals("/presideover/common/download/**") || requestURI.equals("/presideover/schedule/filterSchedule")  || requestURI.equals("/presideover/emcee/listEmcee")  || requestURI.equals("/presideover/applet-user/getHeadAndName/**") || requestURI.equals("/presideover/file/upload/download/**") || requestURI.equals("/files/**") || requestURI.equals("/presideover/common/delectPrcture") || requestURI.equals("/presideover/file/upload/delectVideo")) {
                log.info("该接口令牌不存在也能看");
                return true;
            }

            //创建响应结果对象
            //把Result对象转换为JSON格式字符串 (fastjson是阿里巴巴提供的用于实现对象和json的转换工具类)
            String json = JSONObject.toJSONString(Result.build(600,"令牌不存在"));
            //设置响应头（告知浏览器：响应的数据类型为json、响应的数据编码表为utf-8）
            response.setContentType("application/json;charset=utf-8");
            //响应
            response.getWriter().write(json);
            return false;//不放行

        }

        //5.解析token，如果解析失败，返回错误结果（未登录）
        try {
            JwtUtils.parseJWT(token);
        }catch (ExpiredJwtException e) {
            log.info("令牌过期!");

            // 创建响应结果对象

            // 转换为JSON格式字符串
            String json = JSONObject.toJSONString(Result.build(600,"令牌过期"));

            // 设置响应头
            response.setContentType("application/json;charset=utf-8");

            // 响应
            response.getWriter().write(json);

            return false;
        }catch (Exception e) {
            log.info("令牌解析失败!");

            //创建响应结果对象
//            Result resultInfo = new Result(600,"令牌解析失败",null);
            //把Result对象转换为JSON格式字符串 (fastjson是阿里巴巴提供的用于实现对象和json的转换工具类)
            String json = JSONObject.toJSONString(Result.build(600,"令牌解析失败"));
            //设置响应头
            response.setContentType("application/json;charset=utf-8");
            //响应
            response.getWriter().write(json);
            return false;
        }
        return true;
    }
}
